package android.alliance.ads;

import java.util.Map;

import android.alliance.ads.enumerations.FullscreenTarget;
import android.alliance.ads.interfaces.IBannerCallback;
import android.app.Activity;
import android.content.Intent;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

/**
 * Android Alliance - Ads-Library
 *
 * Copyright (c) 2011-2014 Android Alliance
 *
 * http://www.android-alliance.org
 * http://www.droidspirit.com
 * http://www.strangeoptics.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
 * this file except in compliance with the License. You may obtain a copy of the License at  
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 * 
 *  E-Mail: support@droidspirit.com
 */
public class FullscreenBanner {

	private static FullscreenBanner instance;
	private InterstitialAd interstitialAd;
	private AdRequest adInterstitialRequest;
	private Integer bannerZaehler = null;
	private AdAdapter adAdapter;
	private int bannerResource = 0;
	private String target;
	private Activity parentActivity;
	private IBannerCallback bannerCallback;
	
	public static FullscreenBanner getInstance(){
		if(instance == null){
			instance = new FullscreenBanner();
		}
		
		return instance;
	}
	
	protected void init(Activity parentActivity, String admobKey, IBannerCallback bannerCallback){

		try{
			this.adAdapter = AdAdapter.getInstance();
			this.parentActivity = parentActivity;
			this.bannerCallback = bannerCallback;
			
			interstitialAd = new InterstitialAd(parentActivity);
			interstitialAd.setAdUnitId(admobKey);
			
			initAdMobBanner();			
			
		} catch (Exception e){
			// do nothing
		}
	}
	

	private void initAdMobBanner(){

		try{
			if(adInterstitialRequest == null){
				adInterstitialRequest = new AdRequest.Builder().build();
			}
			
			interstitialAd.setAdListener(new AdListener(){

			@Override
			public void onAdClosed() {
				super.onAdClosed();

				if(bannerCallback != null){
					bannerCallback.bannerClicked(FullscreenTarget.FROM_ADMOB_BANNER);	
				}
			}

			@Override
			public void onAdFailedToLoad(int errorCode) {
				super.onAdFailedToLoad(errorCode);
				loadOwnAds();
			}

			@Override
			public void onAdLeftApplication() {
				super.onAdLeftApplication();
			}

			@Override
			public void onAdLoaded() {
				super.onAdLoaded();
				
				try{

					if(bannerCallback != null){
						bannerCallback.onAdLoaded();	
					}
					
					if (interstitialAd.isLoaded()) {
						interstitialAd.show();
				    }
					
				} catch(Exception e){
					// do nothing
				}
			}

			@Override
			public void onAdOpened() {
				super.onAdOpened();
			}

			
			});
			
			interstitialAd.loadAd(adInterstitialRequest);	
		
		} catch (Exception e){
			// do nothing
		}
	}
	
	protected void initNextFullscreenBanner(){
		try{
			if(bannerZaehler == null || bannerZaehler.intValue() == adAdapter.getAlBannerFullscreen().size() -1){
				bannerZaehler = Integer.valueOf(0);
			} else {
				bannerZaehler++;
			}
			
			int y = 0;
			for(Map.Entry<Integer, String> entry : ((Map<Integer, String>) adAdapter.getAlBannerFullscreen()).entrySet()) {
				if(y == bannerZaehler){
					bannerResource = entry.getKey();
					target = entry.getValue();
					break;	
				}
				y++;
			}
			
		} catch(Exception e){
			// do nothing
		}
	}
	
	protected void clearAdmobView(){
		if(interstitialAd != null){
			interstitialAd.setAdListener(null);
		}
	}
	
	
	public void loadOwnAds(){
		try{

			clearAdmobView();	
			
			// Get next banner
			initNextFullscreenBanner();
						
			Intent startDialog = new Intent(parentActivity, FullscreenDialogActivity.class);
			parentActivity.startActivity(startDialog);
			
		} catch(Exception e){
			// do nothing
		}
	}

	protected int getBannerResource(){
		return bannerResource;
	}
	
	protected String getTarget(){
		return target;
	}
	
	protected IBannerCallback getBannerCallback(){
		return bannerCallback;
	}
}
